package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.driver.config.MinioUtils;
import com.atguigu.daijia.driver.service.FileService;
import com.atguigu.daijia.driver.config.MinioProperties;
import io.minio.MinioClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class FileServiceImpl implements FileService {
    @Resource
    private MinioUtils minioUtils;
    @Resource
    private MinioProperties minioProperties;


    @Override
    public String upload(MultipartFile file) {
        //判断桶是否存在
        minioUtils.existBucket("daijia");//这个方法都不需要判断，util中如果桶不存在的话，它自己会创建一个


        //设置存储对象的名称
        String extFileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = new SimpleDateFormat("yyyyMMdd")
                .format(new Date()) + "/" + UUID.randomUUID().toString().replace("-" , "") + "." + extFileName;

        //上传
        minioUtils.upload(file,fileName);

        return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + fileName ;

    }
}
